#include"../source/server.hpp"

uint64_t conn_id = 0;

std::unordered_map<uint64_t,PtrConnection> _conns;

int next_loop = 0;

EventLoop base_loop;

LoopThreadPool* loop_pool;

void ConnectionDestory(const PtrConnection& conn)
{
    _conns.erase(conn->Id());
}


void OnConnected(const PtrConnection& conn)
{
    DBG_LOG("NEW CONNECTION:%p",conn.get());
}

void OnMessage(const PtrConnection& conn,Buffer* buf)
{
    DBG_LOG("%s",buf->ReadPosition());
    buf->MoveReadOffset(buf->ReadAbleSize());

    std::string str = "i loooooove ghx";
    conn->Send(str.c_str(),str.size());

    conn->Shutdown();
}



void NewConnection(int fd)
{
    conn_id++;
    PtrConnection conn (new Connection(loop_pool->NextLoop(),conn_id,fd));
    conn->SetMessageCallback(std::bind(OnMessage,std::placeholders::_1,std::placeholders::_2));
    conn->SetConnectedCallback(std::bind(OnConnected,std::placeholders::_1));
    conn->SetSrvClosedCallback(std::bind(ConnectionDestory,std::placeholders::_1));

    
    conn->EnableInactiveRelease(5);
    conn->Established();
    _conns.insert(std::make_pair(conn_id,conn));

    DBG_LOG("NEW---------------------");
}

int main()
{
    loop_pool = new LoopThreadPool(&base_loop);
    loop_pool->SetThreadCount(2);
    loop_pool->Create();
    Acceptor acceptor(&base_loop,9658);
    acceptor.SetAcceptCallback(std::bind(NewConnection,std::placeholders::_1));
    acceptor.Listen();
    base_loop.Start();

    return 0;
}